--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[



]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---- 引入界面控件
    local Widget = require "widgets/widget"
    local Screen = require "widgets/screen"
    local Image = require "widgets/image"
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 按钮数据
    local button_data = {
        ----------------------------------------------
        --- 中点
            ["mechanical_liberation"] = { 
                pt = Vector3(30,15,0),
                info = "机械的解放",
                pre_talent = nil,
            },
        ----------------------------------------------
        --- 
            ["evolution_analysis"] = {
                pt = Vector3(-75,50,0),
                info = "进化的解析",
                pre_talent = "mechanical_liberation",
            },
            ["multi_armed_evolution"] = {
                pt = Vector3(-100,130,0),
                info = "多臂的进化",
                pre_talent = "evolution_analysis",                            
            },
            ["evolutionary_electromagnetism"] = {
                pt = Vector3(-125,210,0),
                info = "进化的电磁",
                pre_talent = "multi_armed_evolution",
            },
            ["evolution_holiness"] = {
                pt = Vector3(-150,290,0),
                info = "进化的神圣",
                pre_talent = "evolutionary_electromagnetism",
            },
        ----------------------------------------------
        --- 
            ["ancient_evolution"] = {
                pt = Vector3(-75,-30,0),
                info = "进化的古老",
                pre_talent = "mechanical_liberation",
            },
            ["evolutionary_mystery"] = {
                pt = Vector3(-100,-110,0),
                info = "进化的神秘",
                pre_talent = "ancient_evolution",
            },
            ["evolution_defense"] = {
                pt = Vector3(-135,-190,0),
                info = "进化的守御",
                pre_talent = "evolutionary_mystery",
                icon_offset = Vector3(0,7,0),
            },                            
            ["evolution_subtlety"] = {
                pt = Vector3(-170,-270,0),
                info = "进化的精奥",
                pre_talent = "evolution_defense",
            },
        ----------------------------------------------
        --- 
            ["evolution_sharpness"] = {
                pt = Vector3(-255,210,0),
                info = "进化的锋锐",
                pre_talent = nil,
            },
            ["sharper_I"] = {
                pt = Vector3(-310,130,0),
                info = "更加锋锐I",
                pre_talent = "evolution_sharpness",
            },
            ["sharper_II"] = {
                pt = Vector3(-310,50,0),
                info = "更加锋锐II",
                pre_talent = "sharper_I",
            },
            ["sharper_III"] = {
                pt = Vector3(-210,130,0),
                info = "更加锋锐III",
                pre_talent = "sharper_II",
            },
            ["sharper_IV"] = {
                pt = Vector3(-210,50,0),
                info = "更加锋锐IV",
                pre_talent = "sharper_III",
            },
            ["universal_energy"] = {
                pt = Vector3(-210,-30,0),
                info = "通用能源",
                pre_talent = "sharper_IV",
            },
        ----------------------------------------------                      
        --- 
            ["evolution_rapid_strike"] = {
                pt = Vector3(-280,-160,0),
                info = "进化的迅袭",
                pre_talent = nil,
            },
            ["evolution_armored_rapid_strike"] = {
                pt = Vector3(-280,-240,0),
                info = "进化的武装迅袭",
                pre_talent = "evolution_rapid_strike",
            },
        ----------------------------------------------
        --- 
            ["sound_barrier_break"] = {
                pt = Vector3(120,180,0),
                info = "突破音速",
                pre_talent = "mechanical_liberation",
            },
            ["creation_synchronization"] = {
                pt = Vector3(200,245,0),
                info = "创造物的同步",
                pre_talent = "sound_barrier_break",
            },
            ["mechanical_dog"] = {
                pt = Vector3(280,245,0),
                info = "机械犬",
                pre_talent = "creation_synchronization",
            },
        ----------------------------------------------
        --- 
            ["creation_impact"] = {
                pt = Vector3(120,80,0),
                info = "创造物冲击",
                pre_talent = "mechanical_liberation",
            },
            ["beam_suppression"] = {
                pt = Vector3(200,12,0),
                info = "光束的镇压",
                pre_talent = "creation_impact",
            },
            ["life_mass_production"] = {
                pt = Vector3(280,12,0),
                info = "生命量产",
                pre_talent = "beam_suppression",
            },
        ----------------------------------------------
        ---
            ["energy_saving_mode"] = {
                pt = Vector3(120,-110,0),
                info = "节能模式",
                pre_talent = "mechanical_liberation",
            },
            ["overload_charging"] = {
                pt = Vector3(210,-110,0),
                info = "过载充能",
                pre_talent = "energy_saving_mode",                            
            },
            ["hyper_charging"] = {
                pt = Vector3(300,-110,0),
                info = "超量充能",
                pre_talent = "overload_charging",
            },
            ["capacity_expansion_I"] = {
                pt = Vector3(250,-200,0),
                info = "容量拓展I",
                pre_talent = "hyper_charging",                            
            },
            ["capacity_expansion_II"] = {
                pt = Vector3(170,-200,0),
                info = "容量拓展II",
                pre_talent = "capacity_expansion_I",
            },
            ["capacity_expansion_III"] = {
                pt = Vector3(170,-280,0),
                info = "容量拓展III",
                pre_talent = "capacity_expansion_II",
            },
            ["capacity_expansion_IV"] = {
                pt = Vector3(250,-280,0),
                info = "容量拓展IV",
                pre_talent = "capacity_expansion_III",
            },
        ----------------------------------------------
    }
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.
---- 显示信息
    -- local LANGUAGE = "ch"
    -- if type(TUNING["loramia.Language"]) == "function" then
    --     LANGUAGE = TUNING["loramia.Language"]()
    -- elseif type(TUNING["loramia.Language"]) == "string" then
    --     LANGUAGE = TUNING["loramia.Language"]
    -- end
    -- local function GetDisplayText(talent_name)
    --     if LANGUAGE == "ch" then
    --         return button_data[talent_name].info
    --     else
    --         -- 将下划线替换为空格
    --         local result = talent_name:gsub("_", " ")
    --         -- 将每个单词的首字母大写
    --         result = result:gsub("(%w+)", function(word)
    --             return word:sub(1, 1):upper() .. word:sub(2)
    --         end)
    --         return result
    --     end
    -- end
    local function GetDisplayText(talent_name)
        local cmd_table = TUNING.LORAMIA_FN:GetStringsTable("talent_info",talent_name) or {}
        local talent_display = cmd_table[1] or ""
        local pt = cmd_table[2] or Vector3(0,0,0)
        local talent_info = cmd_table[3] or ""
        return talent_display,pt,talent_info
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 创建界面
    local function create_talent_tree_widget(inst,base_front_root)
                --------------------------------------------------------------------------------
                ---- 
                    -- if TUNING.LORAMIA_DEBUGGING_MODE and inst.__test_tree_fn then
                    --     inst.__test_tree_fn(inst,base_front_root)
                    --     return
                    -- end
                --------------------------------------------------------------------------------
                --- 参数
                    local atlas = "images/widgets/loramia_talent_tree.xml"
                --------------------------------------------------------------------------------
                --- 创建前置根节点
                    local front_root = base_front_root:AddChild(Widget())
                    front_root:SetHAnchor(0) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
                    front_root:SetVAnchor(0) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
                    front_root:SetPosition(0,0)
                    front_root:SetScaleMode(SCALEMODE_FIXEDSCREEN_NONDYNAMIC)   --- 缩放模式
                --------------------------------------------------------------------------------
                --- 创建根节点
                    local root = front_root:AddChild(Widget())
                    local scale = 0.7
                    root:SetScale(scale,scale)
                    root:SetPosition(0,50)
                --------------------------------------------------------------------------------
                --- 背景
                    local bg = root:AddChild(Image(atlas,"bg.tex"))
                --------------------------------------------------------------------------------
                --- 创建按钮
                    local function create_button(talent_name,pt,data)
                        local temp_button = root:AddChild(ImageButton(
                            atlas,
                            "selected_box.tex",
                            "selected_box.tex",
                            "selected_box.tex",
                            "selected_box.tex",
                            "selected_box.tex"
                        ))
                        local icon = temp_button.image:AddChild(Image(atlas,talent_name..".tex"))
                        if data.icon_offset then
                            icon:SetPosition(data.icon_offset.x,data.icon_offset.y)
                        end
                        temp_button:SetPosition(pt.x,pt.y)
                        temp_button.talent_info = data.info
                        temp_button.talent_name = talent_name
                        function temp_button:SetCanClick(flag)
                            local pic = "unselected_box.tex"
                            temp_button:SetTextures(atlas,pic,pic,pic,pic,pic)
                            if flag then
                                temp_button.focus_scale = {1.4, 1.4, 1.4}
                                temp_button:SetOnClick(function()
                                    if root.__click_cd then
                                        return
                                    end
                                    root.__click_cd = root.inst:DoTaskInTime(0.5,function()
                                        root.__click_cd = nil
                                    end)
                                    if ThePlayer.replica.loramia_com_talent_tree then
                                        if not ThePlayer.replica.loramia_com_talent_tree:IsUnlocked(talent_name) then
                                            ThePlayer.replica.loramia_com_talent_tree:Unlock(talent_name)
                                        end
                                    end
                                end)                                
                            else
                                temp_button.focus_scale = {1, 1, 1}
                                temp_button:SetOnClick(nil)
                            end
                        end
                        function temp_button:ShowWaring(flag)
                            if temp_button._clickable_info_bg == nil then
                                temp_button._clickable_info_bg = temp_button.image:AddChild(Image(atlas,"selected_box.tex"))
                                temp_button._clickable_info_bg:MoveToBack()
                                temp_button._clickable_info_bg:SetScale(1.2,1.2)
                            end
                            if flag then
                                temp_button._clickable_info_bg:Show()
                            else
                                temp_button._clickable_info_bg:Hide()
                            end
                        end
                        local pre_talent = data.pre_talent
                        if ThePlayer.replica.loramia_com_talent_tree then
                            if pre_talent == nil or ThePlayer.replica.loramia_com_talent_tree:IsUnlocked(pre_talent) then
                                temp_button:SetCanClick(true)
                                temp_button:ShowWaring(true)
                            else
                                temp_button:SetCanClick(false)
                                temp_button:ShowWaring(false)
                            end
                            if ThePlayer.replica.loramia_com_talent_tree:IsUnlocked(talent_name) then
                                temp_button:SetCanClick(false)
                                temp_button:ShowWaring(false)
                                temp_button:SetTextures(atlas,"selected_box.tex","selected_box.tex","selected_box.tex","selected_box.tex","selected_box.tex")
                            end
                        end
                        temp_button.ongainfocus = function()
                            root.inst:PushEvent("talent_mouseover",talent_name)
                            temp_button:MoveToFront()
                            root.inst:PushEvent("talent_mouseover_refresh")
                        end
                        temp_button.onlosefocus = function()
                            root.inst:PushEvent("talent_mouseout")
                        end
                        return temp_button
                    end
                --------------------------------------------------------------------------------
                ---- 所有按钮
                    root.buttons = {}
                    root.inst:ListenForEvent("refresh_buttons",function()
                        ------------------------------------------------------------------------
                        --- 先删除旧有的
                            for k,v in pairs(root.buttons) do
                                v:Kill()
                            end
                            root.buttons = {}
                        ------------------------------------------------------------------------
                        --- 创建新的
                            for k,data in pairs(button_data) do
                                local talent_name = k
                                local pt = data.pt
                                local temp_button = create_button(talent_name,pt,data)
                                table.insert(root.buttons,temp_button)
                            end
                        ------------------------------------------------------------------------
                    end)
                    root.inst:PushEvent("refresh_buttons")
                    root.inst:ListenForEvent("loramia_event.talent_tree_client_side_updated",function()
                        root.inst:PushEvent("refresh_buttons")
                        root.inst:PushEvent("talent_mouseover_refresh")
                        root.inst:PushEvent("refresh_talent_points")
                    end,inst)
                --------------------------------------------------------------------------------
                --- 点数
                    local talent_points = root:AddChild(Text(CODEFONT,40,"0",{255/255,255/255,255/255,1}))
                    talent_points:SetPosition(-330,300)
                    local function refresh_talent_points()
                        local num = ThePlayer.replica.loramia_com_talent_tree and ThePlayer.replica.loramia_com_talent_tree:GetPoints() or 0
                        talent_points:SetString(num)
                        if num == 0 then
                            for k, temp_button in pairs(root.buttons) do
                                if ThePlayer.replica.loramia_com_talent_tree and not ThePlayer.replica.loramia_com_talent_tree:IsUnlocked(temp_button.talent_name) then
                                    temp_button:ShowWaring(false)
                                end
                            end
                        end
                    end
                    refresh_talent_points()
                    talent_points.inst:DoPeriodicTask(0.1,refresh_talent_points)
                    root.inst:ListenForEvent("refresh_talent_points",refresh_talent_points)
                --------------------------------------------------------------------------------
                --- 重置按钮
                    local reset_button = root:AddChild(ImageButton(
                        atlas,
                        "selected_box.tex",
                        "selected_box.tex",
                        "selected_box.tex",
                        "selected_box.tex",
                        "selected_box.tex"
                    ))
                    reset_button:SetScale(0.5,0.5)
                    reset_button:SetPosition(-290,300)
                    local icon = reset_button.image:AddChild(Image("images/global_redux.xml","wardrobe_reset.tex"))
                    local icon_scale = 2
                    icon:SetScale(icon_scale,icon_scale)
                    reset_button:SetOnClick(function()
                        if ThePlayer.replica.loramia_com_talent_tree then
                            ThePlayer.replica.loramia_com_talent_tree:Reset()
                        end
                    end)
                --------------------------------------------------------------------------------
                --- mouse over text
                    local mouser_over_front = root:AddChild(Widget())
                    mouser_over_front:SetHAnchor(1) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
                    mouser_over_front:SetVAnchor(2) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
                    local mouse_over_text = mouser_over_front:AddChild(Text(CODEFONT,40,"",{255/255,255/255,255/255,1}))
                    mouse_over_text:SetPosition(100,-50)
                    root.inst:ListenForEvent("talent_mouseover",function(_,talent_name)
                        local name,info_pt,info_str = GetDisplayText(talent_name)
                        mouse_over_text:SetString(name)
                        root.inst:PushEvent("talent_info_box",{
                            pt = info_pt,
                            info = info_str,
                        })
                    end)
                    root.inst:ListenForEvent("talent_mouseout",function()
                        mouse_over_text:SetString("")
                    end)
                    mouser_over_front:FollowMouse()
                    root.inst:ListenForEvent("talent_mouseover_refresh",function()
                        mouser_over_front:MoveToFront()
                    end)
                --------------------------------------------------------------------------------
                --- 天赋信息（背景）
                    local info_box = root:AddChild(Image("images/widgets/loramia_talent_info_box.xml","loramia_talent_info_box.tex"))
                    info_box:SetPosition(0,-400)
                --------------------------------------------------------------------------------
                --- 天赋信息（文本）
                    local talent_info = info_box:AddChild(Text(CODEFONT,40,"",{255/255,255/255,255/255,1}))
                    talent_info:SetPosition(0,0)
                    -- talent_info:SetString("44444444444")
                    root.inst:ListenForEvent("talent_info_box",function(_,_table)
                        talent_info:SetString(_table.info)
                        talent_info:SetPosition(_table.pt.x,_table.pt.y)
                    end)
                --------------------------------------------------------------------------------
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    inst:ListenForEvent("loramia_widget_display_talent_tree",create_talent_tree_widget)
    if not TheWorld.ismastersim then
        return
    end

end